/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.resourcemaster.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.Observable;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultMutableTreeNode;

import wzw.beans.CodeNameBean;
import wzw.sql.DatabaseOperator;
import wzw.swing.table.IconTableCellRenderer;
import wzw.swing.table.PlainTable;
import wzw.swing.table.PlainTableModel;
import wzw.swing.table.ZebraCrossTableCellRenderer;
import wzw.swing.tree.PlainTree;
import wzw.swing.tree.PlainTreeModel;
import wzw.util.I18nUtil;
import wzw.util.I18nUtils;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.ui.DirectorFrame;
import com.kingcore.director.base.ui.PlainDirectorPanel;
import com.kingcore.director.core.util.DirectorComponentUtils;
import com.kingcore.director.main.Main;

/**
 * <p>java类文件的说明...</p>
 * @author Zeven/wzw on Jul 23, 2011
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class ProgramLibraryMainPanel extends PlainDirectorPanel implements DatabaseOperator,
		ItemListener, ActionListener, MouseListener, ChangeListener, KeyListener{

	public ProgramLibraryMainPanel(DirectorFrame baseFrame) {
		super(baseFrame);
		initializeInterface();
		
		this.update(null, null);
		constructAfter();
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final int MainTable_Column_Count = 6;
	private static final int MainTable_Column_Index_Name = 0;
	private static final int MainTable_Column_Index_Describe = 1;
	private static final int MainTable_Column_Index_Size = 2;
	private static final int MainTable_Column_Index_Score = 3;
	private static final int MainTable_Column_Index_LocalExist = 4;
	private static final int MainTable_Column_Index_InFavorite = 5;
	private static final int MainTable_Column_Index_Uri = 3;

	public final static int MainSplitPane_Left_Width = 170;
	public static final int[] MainTable_Columns_Width = {200,Main.MainFrame_Size_Width
						-MainSplitPane_Left_Width-200-80-80-100-100, 80, 80,100,100};
	public static final String[] MainTable_Headers = {"Name", "Describe", "Size", "Score", "LocalExist","InFavorite"};
	public static final Object[] MainTable_Sample_Data = {"火狐浏览器", "广泛使用的安全浏览器", "12.6M", "9.5", "N   (下载)", "N"};
	

	private JSplitPane mainSplitPane = null;
	private JScrollPane leftScrollPane = null;
	private JSplitPane rightSplitPane = null;
	private JScrollPane rightScrollPane = null;
	private PlainTable mainTable = null;
	private PlainTree mainTree = null;
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	protected void initializeInterface() {
		// BorderLayout ...
		this.setLayout( new BorderLayout() );
		//this.add( getMainNorthPanel(), BorderLayout.NORTH);
		
		mainSplitPane = getMainSplitPane();
		this.add( mainSplitPane, BorderLayout.CENTER);
		this.add( getStatusBar(true), BorderLayout.SOUTH);

		leftScrollPane = getLeftScrollPane();
		rightSplitPane = getRightSplitPane();
		mainSplitPane.setLeftComponent(leftScrollPane);
		mainSplitPane.setRightComponent(rightSplitPane);
		mainSplitPane.setDividerLocation( MainSplitPane_Left_Width );  //200);
		
		leftScrollPane.setViewportView( getMainTree() );	//getJt_tree()
		rightScrollPane = getRightScrollPane();
		rightScrollPane.setViewportView( getMainTable() ); //getTable()
		//this.add(new JLabel("提供云服务-程序分类，热门程序推荐下载，并且与本地文件、FavoriteFiles相关联。"), BorderLayout.NORTH);
	}

	/**
	 * This method initializes table	
	 * 	
	 * @return javax.swing.JTable	
	 */
	protected PlainTable getMainTable(){
		if (mainTable==null) {
			mainTable = new PlainTable();	 
		    Object[][] data = null;
		    
		    // A DefaultTableModel object is created and initialized with some
		    // data and headers.

		    //DefaultTableModel dtm = new DefaultTableModel(2, 3);
		    //DefaultTableModel dtm = new UneditableTableModel(2, 3);
		    DefaultTableModel dtm = new DefaultTableModel(2, 3); //no new methods
		    dtm.setDataVector(data, MainTable_Headers );
		    PlainTableModel sorter = new PlainTableModel( dtm, mainTable.getTableHeader());
//		    sorter.setColumnEditable( MainTable_Column_Index_Name );
//		    RowSorter sorter = new TableRowSorter(dtm);
		    sorter.setColumnClass(MainTable_Column_Index_Size, java.lang.Integer.class);
		    mainTable.setModel(sorter);
		    
//		    table.setRowSorter(sorter);
		    
		    mainTable.setGridColor(Color.LIGHT_GRAY);
		    mainTable.setRowSelectionAllowed(true);
		    if (mainTable.getRowCount()>0) {
			    mainTable.setRowSelectionInterval(0, 0);	
			}
		    mainTable.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		    mainTable.setShowGrid(true);
//		    mainTable.setRowHeight(21);
//		    mainTable.getTableHeader().setReorderingAllowed(false);
		    mainTable.addMouseListener( this );
		    mainTable.addKeyListener( this );
		     
		    // 更改列宽度
//		    setAllColWith(table);
		    TableColumn tc = null;
		    TableColumnModel tcm = mainTable.getColumnModel(); 
	        for(int i=0; i <MainTable_Columns_Width.length; i++)   {
	            tc   =   tcm.getColumn(i);
	            tc.setPreferredWidth(MainTable_Columns_Width[i]); 
	            
	        }
	        
	        this.getMainTable().showZebraCross();

	        // column size to right
	        tc = tcm.getColumn( MainTable_Column_Index_Size );//column name
	        DefaultTableCellRenderer renderer = new ZebraCrossTableCellRenderer();
	        renderer.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
	        tc.setCellRenderer(renderer);
	        
	        TableCellRenderer myRenderer = new IconTableCellRenderer(mainTable, MainTable_Column_Index_Uri, true);
	        tc = tcm.getColumn( MainTable_Column_Index_Name );//column name
	       /// tc.setCellRenderer(myRenderer);
	        //mainTable.getColumnModel().removeColumn(mainTable.getColumn(MainTable_Column_Index_Uri));
	        int[] cols = {MainTable_Column_Index_Uri};
	        DirectorComponentUtils.hideTableColumns(mainTable, cols); 
	        (this.getMainTable().getModel()).getTableModel().addRow(MainTable_Sample_Data);
	         
	        //mainTable.getModel().addTableModelListener(this);
		}
		return mainTable;
		
	}
	
	/**
	 * 
	 */
	protected PlainTree getMainTree() {
		if (mainTree  == null) {
			//jTree = new JTree();
			//jTree.setBounds(new java.awt.Rectangle(7,7,154,241));
			//add wzw
		   
			//jtf = new JTextField(15);
		    //jtf.setEditable(false);

		    // Eight DefaultMutableTreeNode objects are created

		    DefaultMutableTreeNode root = 
		      new DefaultMutableTreeNode(new CodeNameBean("","WZW Director") );
		    
		    DefaultMutableTreeNode dir = 
			      new DefaultMutableTreeNode( new CodeNameBean("aa","我的提醒"));
		    DefaultMutableTreeNode ord = 
		      new DefaultMutableTreeNode( new CodeNameBean("0000","装机必备"));
		    DefaultMutableTreeNode imp = 
		      new DefaultMutableTreeNode( new CodeNameBean("0001","今日热门"));
		    DefaultMutableTreeNode bri = 
		      new DefaultMutableTreeNode( new CodeNameBean("0002","聊天"));
		    DefaultMutableTreeNode ply = 
		      new DefaultMutableTreeNode( new CodeNameBean("0003","视频"));
			DefaultMutableTreeNode ait = 
			      new DefaultMutableTreeNode( new CodeNameBean("0004","浏览器"));
			DefaultMutableTreeNode els0 = 
			      new DefaultMutableTreeNode( new CodeNameBean("0005","音乐"));
			DefaultMutableTreeNode els1 = 
			      new DefaultMutableTreeNode( new CodeNameBean("0005","游戏"));
			DefaultMutableTreeNode els2 = 
			      new DefaultMutableTreeNode( new CodeNameBean("0005","安全杀毒"));
			DefaultMutableTreeNode els3 = 
			      new DefaultMutableTreeNode( new CodeNameBean("0005","输入法"));
			DefaultMutableTreeNode els4 = 
			      new DefaultMutableTreeNode( new CodeNameBean("0005","股票网银"));
			DefaultMutableTreeNode els5 = 
			      new DefaultMutableTreeNode( new CodeNameBean("0005","行业软件"));
			

		    DefaultMutableTreeNode gbg = 
			      new DefaultMutableTreeNode( new CodeNameBean("","垃圾箱"));

		    // The node hierarchy is loaded into a Default TreeModel object using
		    // the insertNodeInto() method.
		    
		    //wzw 
		    //dir.
		    
		    PlainTreeModel dtm = new PlainTreeModel(root);
//		    dtm.insertNodeInto(dir, root, 0);
//		    dtm.insertNodeInto(gbg, root, 1);
		    
		    dtm.insertNodeInto(ord, root, 0);
		    dtm.insertNodeInto(imp, root, 1);
		    dtm.insertNodeInto(bri, root, 2);
		    dtm.insertNodeInto(ply, root,3);
		    dtm.insertNodeInto(ait, root,4);
		    
		    dtm.insertNodeInto(els5, root,5);
		    dtm.insertNodeInto(els4, root,5);
		    dtm.insertNodeInto(els3, root,5);
		    dtm.insertNodeInto(els2, root,5);
		    dtm.insertNodeInto(els1, root,5);
		    dtm.insertNodeInto(els0, root,5);

		    //garbage下面的子节点
		    //dtm.insertNodeInto(ord, gbg, 0);
		    //dtm.insertNodeInto(imp, gbg, 1);
		    
		    //dtm.insertNodeInto(Bob, fin, 0);
		    //dtm.insertNodeInto(Sue, fin, 1);
		    //dtm.insertNodeInto(Joe, fin, 2);
		    
		    //dtm.insertNodeInto(Moe, acc, 0);

		    // A JTree object is created using the previously created
		    // DefaultTreeModel object.  Root handles are shown and the
		    // row at index 1 is expanded.

		    mainTree = new PlainTree(dtm);
           /// mainTree.setCellRenderer(new DirectorRenderer() );
		    mainTree.setShowsRootHandles(true);
		    mainTree.setRootVisible(false);
		    mainTree.expandRow(0);		//1
		    
		    // if there is a ScrollPanel,don't set Border again.
			//mainTree.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
		    ///mainTree.addMouseListener(new MainFrame_tree_mouseAdapter(this));
		    mainTree.addKeyListener(this);
		}
		return mainTree;
	}


	private JPopupMenu popupMenuForTable = null;
	private JMenuItem jpmi_MainTable_GoDownload = null;
	private JMenuItem jpmi_MainTable_AddToFavoriteFiles = null;
	private JMenuItem jpmi_MainTable_Scoring = null;
	/**
	 * Table的弹出式菜单
	 */
	private JPopupMenu getPopupMenuForMainTable(){
		
		if(popupMenuForTable == null){
			popupMenuForTable = new JPopupMenu();
			
			jpmi_MainTable_GoDownload = new JMenuItem();
			jpmi_MainTable_GoDownload.setText("到天空软件下载");
			jpmi_MainTable_GoDownload.addActionListener( this );
			jpmi_MainTable_AddToFavoriteFiles = new JMenuItem();
			jpmi_MainTable_AddToFavoriteFiles.setText("加入到我的常用列表");
			jpmi_MainTable_AddToFavoriteFiles.addActionListener( this );
			jpmi_MainTable_Scoring = new JMenuItem();
			jpmi_MainTable_Scoring.setText("给程序打分");
			jpmi_MainTable_Scoring.addActionListener( this );
			
			popupMenuForTable.add( jpmi_MainTable_GoDownload );
			popupMenuForTable.add( jpmi_MainTable_AddToFavoriteFiles );
			popupMenuForTable.add( jpmi_MainTable_Scoring );
		}
		
		return popupMenuForTable;
	}
	
	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on Jul 23, 2011
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public static final String TianKong_Download_URL = "http://www.skycn.com/";
	public void actionPerformed(ActionEvent event) {
		Object obj=event.getSource();
		if (obj == jpmi_MainTable_GoDownload) {
			DirectorComponentUtils.openWebUrl(TianKong_Download_URL);
		}
	}
	
	@Override
	public boolean fetchData() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean saveData() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		Object obj = e.getSource() ;
		if(obj==getMainTable()){
			// 在Table上右击鼠标
			//System.out.println ( " getPopupMenuForTable 1");
			if( e.getButton() == MouseEvent.BUTTON3 ) {	//显示弹出菜单
				
				//System.out.println (getTable().getX()+e.getX());
				//getPopupMenuForTable().locate( getTable().getX()+e.getX(), getTable().getY()+e.getY() );
				//getPopupMenuForTable().setVisible( true );
				getPopupMenuForMainTable().setSelected(null);
				getPopupMenuForMainTable().show( e.getComponent(), e.getX(), e.getY() );
				
			}
		}
		
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Observable o, Object arg) {
		super.update(o, arg);
		
		//System.out.println(Locale.getDefault());
		ResourceBundle bundle = I18nUtils.getBundle(DirectorConstants.I18n_Language_Name);
		
		//table
	    Vector<String> columnIdentifiers = new Vector<String>();
	    columnIdentifiers.add(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "table.col00.Text"));
	    columnIdentifiers.add(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "table.col01.Text"));
	    columnIdentifiers.add(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "table.col02.Text"));
	    columnIdentifiers.add(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "table.col03.Text"));
	    columnIdentifiers.add(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "table.col04.Text"));
	    columnIdentifiers.add(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "table.col05.Text"));
	    DefaultTableModel dtm = (this.getMainTable().getModel()).getTableModel();
	    dtm.setColumnIdentifiers(columnIdentifiers);
	    
	    //resize table columns
	    TableColumn tc = null;
	    TableColumnModel tcm = mainTable.getColumnModel(); 
        for(int i=0; i < MainTable_Columns_Width.length-1; i++)   { // ?-1
            tc   =   tcm.getColumn(i);
            tc.setPreferredWidth(MainTable_Columns_Width[i]); 
        }
        this.getMainTable().showZebraCross();
        
        // popup menu for table 
        getPopupMenuForMainTable();
        jpmi_MainTable_AddToFavoriteFiles.setText(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "jpmi_MainTable_AddToFavoriteFiles.Text"));
        jpmi_MainTable_GoDownload.setText(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "jpmi_MainTable_GoDownload.Text"));
        jpmi_MainTable_Scoring.setText(I18nUtil.getBundleString(bundle, ProgramLibraryMainPanel.class, "jpmi_MainTable_Scoring.Text"));
        
	}

}
